package com.ruoyi.project.module.whd;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.library.api.ISysDictDataService;
import com.ruoyi.library.api.domain.SysDictData;
import com.ruoyi.project.domain.HeEventFlow;
import com.ruoyi.project.domain.HeEventReport;
import com.ruoyi.project.service.IHeEventFlowService;
import com.ruoyi.project.service.IHeEventReportService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;

/**
 * 事件上报信息Controller
 *
 * @author ruoyi
 * @date 2023-09-02
 */
@RestController
@RequestMapping("/whd/shenhe")
public class WhdshenheController extends BaseController {
    @Autowired
    private IHeEventReportService heEventReportService;
    @Autowired
    private IHeEventFlowService heEventFlowService;


    /**
     * 查询事件上报信息列表
     */
//    @RequiresPermissions("project:report:list")
    @GetMapping("/list")
    public TableDataInfo list(HeEventReport heEventReport) {
        startPage();
        List<HeEventReport> list = heEventReportService.selectHeEventReportList(heEventReport);
        return getDataTable(list);
    }

    /**
     * 获取事件上报信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(heEventReportService.selectHeEventReportById(id));
    }

    /**
     * 新增事件上报信息
     */
    @Log(title = "事件上报信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HeEventReport heEventReport) {
        return toAjax(heEventReportService.insertHeEventReport(heEventReport));
    }

    //调用字典
    @GetMapping("/deptlist")
    public TableDataInfo deptList(HeEventReport heEventReport) {
        startPage();
        List<HeEventReport> list = heEventReportService.selectHeEventReportListBySupportDepartment(heEventReport);
        SysDictData dictData = new SysDictData();

        return getDataTable(list);
    }

    /**
     * 审核事件
     */
    @PutMapping
    public AjaxResult edit(@RequestBody HeEventReport heEventReport) {
        //获取登录人的userid
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        heEventReport.setUserId(sysUser.getUserId());
        String pin;
        //修改事件上报表
        //修改事件上报状态为待审核
        if(heEventReport.getReviewStatus().equals("02"))
        {
            heEventReport.setReportEventState("03");
            pin = sysUser.getNickName() + "审核通过了该事件" ;
            heEventReport.setHandlingStatus("01");
        }
        else if(heEventReport.getReviewStatus().equals("01")){
            heEventReport.setReportEventState("01");
            pin = sysUser.getNickName() + "退回了该事件。"+"  原因："+heEventReport.getReviewAdvice() ;
            heEventReport.setFallbackPersonId(sysUser.getUserId());
            heEventReport.setFallbackPerson(sysUser.getNickName());


        }else{
            heEventReport.setReportEventState("05");
            pin = sysUser.getNickName() + "作废了该事件。"+"  原因："+heEventReport.getInvalidationReason() ;
            heEventReport.setInvalidationPersonId(sysUser.getUserId());
            heEventReport.setInvalidationPerson(sysUser.getNickName());
        }
        HeEventFlow heEventFlow = new HeEventFlow();
        //将分析表的reporteventid注入到flow表的reporteventid中
        heEventFlow.setReportEventId(heEventReport.getId());
        heEventFlow.setCreationTime(DateUtils.getNowDate());
        heEventFlow.setProcessStreamContent(pin);
        heEventFlowService.insertHeEventFlow(heEventFlow);
        return toAjax(heEventReportService.updateHeEventReport(heEventReport));
    }

    /**
     * 处理建议
     */
    @PutMapping("/jianyi")
    public AjaxResult editjianyi(@RequestBody HeEventReport heEventReport) {
        //获取登录人的userid
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        heEventReport.setUserId(sysUser.getUserId());
        //获取当前时间
        heEventReport.setCreationTime(DateUtils.getNowDate());
        String pin = sysUser.getNickName() + "处理了建议";
        //新建个流转表对象
        HeEventFlow heEventFlow = new HeEventFlow();
        //将分析表的reporteventid注入到flow表的reporteventid中
        heEventFlow.setReportEventId(heEventReport.getId());
        heEventFlow.setCreationTime(DateUtils.getNowDate());
        heEventFlow.setProcessStreamContent(pin);
        heEventFlowService.insertHeEventFlow(heEventFlow);
        return toAjax(heEventReportService.updateHeEventReport(heEventReport));
    }

    /**
     * 结案
     */
    @PutMapping("/jiean")
    public AjaxResult editjiean(@RequestBody HeEventReport heEventReport) {
        //获取登录人的userid
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        heEventReport.setUserId(sysUser.getUserId());
        //获取当前时间
        heEventReport.setCreationTime(DateUtils.getNowDate());
        String pin = sysUser.getNickName() + "结案了该事件";
        //新建个流转表对象
        HeEventFlow heEventFlow = new HeEventFlow();
        //将分析表的reporteventid注入到flow表的reporteventid中
        heEventFlow.setReportEventId(heEventReport.getId());
        heEventFlow.setCreationTime(DateUtils.getNowDate());
        heEventFlow.setProcessStreamContent(pin);
        heEventFlowService.insertHeEventFlow(heEventFlow);
        return toAjax(heEventReportService.updateHeEventReport(heEventReport));
    }
}
